package com.jjb.common.view.draw;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class ImageDrawCanvas implements GrawCanvas {

    /**
     * 绑定图片的路径
     */
    private final File file;

    //水印的宽度
    private Integer width;
    //水印的高度
    private Integer height;
    private final int x ;
    private final int y;

    /**
     * 添加水印
     * @param file:水印文件
     * @param x：水印的X坐标
     * @param y：水印的Y坐标
     */
    public ImageDrawCanvas(File file, int x, int y) {
        this.file = file;
        this.x = x;
        this.y = y;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }


    public void draw(Graphics2D g) throws IOException{
        Image srcImg = ImageIO.read(this.file);//文件转化为图片
        if(this.width == null) {
            this.width = srcImg.getWidth(null);
        }
        if(this.height == null) {
            this.height = srcImg.getHeight(null);
        }

        Image image = srcImg.getScaledInstance(this.width, this.height, Image.SCALE_SMOOTH);

        g.drawImage(image, x, y, null);
    }
}
